//package com.colorfruit.framework.rabbitmq.config;
//
//import com.colorfruit.framework.rabbitmq.component.ProducerAckComponent;
//import com.colorfruit.framework.rabbitmq.model.BusinessMsg;
//import lombok.extern.slf4j.Slf4j;
//import org.springframework.amqp.AmqpException;
//import org.springframework.amqp.core.Message;
//import org.springframework.amqp.core.MessageDeliveryMode;
//import org.springframework.amqp.core.MessagePostProcessor;
//import org.springframework.amqp.rabbit.core.RabbitTemplate;
//import org.springframework.amqp.rabbit.support.CorrelationData;
//import org.springframework.beans.a_factory.InitializingBean;
//import org.springframework.beans.a_factory.annotation.Autowired;
//
//@Slf4j
//public  class BaseProducer  implements RabbitTemplate.ConfirmCallback, InitializingBean  {
//
//    @Autowired
//    protected ProducerAckComponent producerAckComponent;//生产者业务类
//
//    @Autowired
//    protected RabbitTemplate rabbitTemplate; //rabbit模板
//
//    protected String producerName; //生产者名称
//
//    /**
//     * 发送消息
//     * @param routingKey 队列名
//     * @param message 消息
//     */
//    public void send( String routingKey, BusinessMsg<String, Object> message){
//        if(message != null) {
//            CorrelationData correlationData = getCorrelationData(message);
//            rabbitTemplate.convertAndSend(RabbitmqConfig.DiRECT_EXCHANGE, routingKey, message, new MessagePostProcessor(){
//                @Override
//                public Message postProcessMessage(Message message) throws AmqpException {
//                    message.getMessageProperties().setDeliveryMode(MessageDeliveryMode.PERSISTENT);
//                    return message;
//                }
//            },correlationData);
//            log.info("send a message: " + message);
//        }
//    }
//
//    /**
//     * 回调确认
//     * @param correlationData 确认数据ID
//     * @param ack true 发送成功 false 发送失败
//     * @param cause
//     */
//    @Override
//    public void confirm(CorrelationData correlationData, boolean ack, String cause) {
//        if (correlationData == null) {
//            return;
//        }
//        if (ack) {
//            producerAckComponent.ackSuccessResolve(producerName, correlationData.getId());
//        } else {
//            producerAckComponent.ackFailedResolve(producerName, correlationData.getId());
//        }
//    }
//
//    /**
//     * 设置生产者名称
//     * 设置回调
//     * @throws Exception
//     */
//
//    @Override
//    public void afterPropertiesSet() throws Exception {
//        //设置生产者名称
//        String className = this.getClass().getSimpleName();
//        String oldFirst = className.substring(0, 1);
//        String newFirst = oldFirst.toLowerCase();
//        this.producerName = className.replaceFirst(oldFirst, newFirst);
//        //设置回调
//        rabbitTemplate.setConfirmCallback(this);
//    }
//
//    /**
//     * 将发送信息存入redis数据库
//     * @param message
//     * @return
//     */
//    protected CorrelationData getCorrelationData(BusinessMsg<String, Object> message) {
//        return producerAckComponent.generateCorrelation(message, this.producerName);
//    }
//}
